package com.zby.charity.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zby.charity.common.BaseResponse;
import com.zby.charity.common.ErrorCode;
import com.zby.charity.common.ResultUtils;
import com.zby.charity.entity.Dto.LectureJoin;
import com.zby.charity.entity.LectureCountEntity;
import com.zby.charity.entity.LectureEntity;
import com.zby.charity.entity.UserEntity;
import com.zby.charity.exception.BusinessException;
import com.zby.charity.service.LectureCountService;
import com.zby.charity.service.LectureService;
import com.zby.charity.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zby
 * @email 2069189112@qq.com
 * @date 2025-01-25 00:16:25
 */
@Slf4j
@RestController
@RequestMapping("charity/lecturecount")
public class LectureCountController {
    @Autowired
    private UserService userService;
    @Autowired
    private LectureService lectureService;
    @Autowired
    private LectureCountService lectureCountService;

    @PostMapping("/join")
    public BaseResponse<Boolean> join(@RequestBody LectureJoin lectureJoin, HttpServletRequest request) {
        int lid = lectureJoin.getLid();
        int uid = lectureJoin.getUid();

        // 参数校验
        if (uid <= 0 || lid <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        // 获取登录用户
        UserEntity loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        if (!loginUser.getId().equals(uid))
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        // 获取讲座信息
        LectureEntity lecture = lectureService.getById(lid);
        if (lecture == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        // 检查用户是否已报名
        QueryWrapper<LectureCountEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid).eq("lid", lid);
        if (lectureCountService.getOne(queryWrapper) != null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "您已经报名过了");
        }

        // 检查讲座人数是否已满
        Integer maxCount = lecture.getCount();
        Integer currentCount = Math.toIntExact(lectureCountService.
                count(new QueryWrapper<LectureCountEntity>().eq("lid", lid)));
        if (currentCount >=maxCount) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "报名人数已满");
        }

        // 创建报名记录
        LectureCountEntity lectureCountEntity = new LectureCountEntity();
        BeanUtils.copyProperties(lectureJoin, lectureCountEntity);
        boolean save = lectureCountService.save(lectureCountEntity);

        if (!save) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "报名失败");
        }

        return ResultUtils.success(true);
    }
    @PostMapping("/judgeJoin")
    public BaseResponse<Boolean> judgeJoin(@RequestBody LectureJoin lectureJoin) {
        int lid = lectureJoin.getLid();
        int uid = lectureJoin.getUid();

        // 参数校验
        if (uid <= 0 || lid <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper<LectureCountEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid).eq("lid", lid);
        if (lectureCountService.getOne(queryWrapper) != null) {
            return ResultUtils.success(true);
        } else {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR, "您未报名");
        }
    }

    @PostMapping("/cancelJoin")
    public BaseResponse<Boolean> cancelJoin(@RequestBody LectureJoin lectureJoin) {
        int lid = lectureJoin.getLid();
        int uid = lectureJoin.getUid();

        // 参数校验
        if (uid <= 0 || lid <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper<LectureCountEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid).eq("lid", lid);
        boolean remove = lectureCountService.remove(queryWrapper);
        return ResultUtils.success(remove);
    }
    @GetMapping("/deleteLectureJoin")
    public BaseResponse<Boolean> deleteLectureJoin(@RequestParam int lid) {
        if (lid <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = lectureCountService.remove(new QueryWrapper<LectureCountEntity>().eq("lid", lid));
        if (!b)
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }
}

